#!/bin/sh
# * this script is just a *temporary* hack to easily
#   enable or disable the tap to click function
# * it does not work in all cases but it does work in all my laptops.

if [ "$1" = "start" ] ; then
	if [ -f /var/local/synaptics_checked ] ; then
		flsynclient -s
		exit
	fi
fi

#check if synaptics driver is loaded
TOUCHPAD_SETTINGS="$(synclient -l 2>&1)"
if [ $? -ne 0 ] ; then
	echo "$TOUCHPAD_SETTINGS" #errmsg
	exit 1
fi

#---------------------------------------

#dialog v1
DV=1

if [ $DV -eq 1 ] ; then

	T2C=$(echo "$TOUCHPAD_SETTINGS" | grep 'TapButton1' | awk '{print $3}')
	if [ $T2C -eq 1 ] ; then
		T2C_ENABLED=1
		DISABLE_TAP_TO_CLICK=1
		NO_LABEL="$(gettext 'Disable')"
		MSG1="$(gettext 'Tap To Click is currently: enabled')"
	else
		ENABLE_TAP_TO_CLICK=1
		NO_LABEL="$(gettext 'Enable')"
		MSG1="$(gettext 'Tap To Click is currently: disabled')"
	fi

	TITLE="$(gettext 'Touchpad settings')"
	CANCEL_LABEL="$(gettext 'Close')"

	pupdialog --colors --background '#FFFF80' --backtitle "$TITLE" \
		--yes-label "$CANCEL_LABEL" \
		--no-label "$NO_LABEL" --yesno "$MSG1" 0 0
	retval=$? #; echo $retval ; exit
	case $retval in
		1) CLICK_YES=1 ;;
		*) unset ENABLE_TAP_TO_CLICK DISABLE_TAP_TO_CLICK ;;
	esac

	if [ $ENABLE_TAP_TO_CLICK ] && [ $CLICK_YES ] ; then
		TapButton1=1
		TapButton2=2
		TapButton3=3
	fi

	if [ $DISABLE_TAP_TO_CLICK ]  && [ $CLICK_YES ] ; then
		TapButton1=0
		TapButton2=0
		TapButton3=0
	fi

else
# dialog v2

	TITLE="$(gettext 'Touchpad settings')"
	MSG1="$(gettext 'You can enable or disable the Tap to Click function.. 
(for more options run "flsynclient")')"
	if [ ! -f ~/.flSynclient ] ; then
		MSG2="$(gettext '

Click cancel for the default behavior..')"
	fi
	MSGX="$MSG1 $MSG2"
	YES_LABEL="$(gettext 'Enable')"
	NO_LABEL="$(gettext 'Disable')"
	CANCEL_LABEL="$(gettext 'Cancel')"

	pupdialog --colors --background '#FFFF80' --backtitle "$TITLE" \
		--yes-label "$CANCEL_LABEL" \
		--extra-button --extra-label "$YES_LABEL" \
		--no-label "$NO_LABEL" --yesno "$MSGX" 0 0
	retval=$? #; echo $retval
	case $retval in
		3) ENABLE_TAP_TO_CLICK=1 ;;
		1) DISABLE_TAP_TO_CLICK=1 ;;
	esac

	if [ $ENABLE_TAP_TO_CLICK ] ; then
		TapButton1=1
		TapButton2=2
		TapButton3=3
	fi

	if [ $DISABLE_TAP_TO_CLICK ] ; then
		TapButton1=0
		TapButton2=0
		TapButton3=0
	fi

fi

#-----------------------------------------------------

if [ $ENABLE_TAP_TO_CLICK ] || [ $DISABLE_TAP_TO_CLICK ] ; then
	[ -f ~/.flSynclient ] || echo "$TOUCHPAD_SETTINGS" > ~/.flSynclient
	sed -i -e "s|TapButton1.*|TapButton1              = ${TapButton1}|" \
		-e "s|TapButton2.*|TapButton2              = ${TapButton2}|" \
		-e "s|TapButton3.*|TapButton3              = ${TapButton3}|" ~/.flSynclient
fi

flsynclient -s

touch /var/local/synaptics_checked

### END ###
